﻿using System;
using System.ComponentModel;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using CRM.Enums;
using CRM.Models.Email;
using Newtonsoft.Json;

namespace CRM.Models
{
    [Table("oem")]
    [Comment("OEM管理_OEM信息")]
    [Index(nameof(Name), Name = "Name")]
    [Index(nameof(State), Name = "State")]
    public class Oem : BaseModel
    {
        public Oem()
        {
        }
        
        [Column("id"), Key, Required]
        public int Id { get; set; }

        [Column("name", TypeName = "varchar(255)"), Comment("OEM名称"), Required]
        public string Name { get; set; }

        [Column("title", TypeName = "varchar(255)"), Comment("标题"), Required]
        public string Title { get; set; }

        [Column("style", TypeName = "tinyint"), Comment("文字样式 1单行 2双行"), Required]
        public Types Style { get; set; } = Types.SingleRow;
        public enum Types { None, SingleRow, DoubleRow }

        [Column("domain", TypeName = "varchar(255)"), Comment("域名(不带http)"), Required]
        public string Domain { get; set; }

        [Column("state", TypeName = "tinyint"), Comment("状态1启用2禁用"), Required]
        public States State { get; set; } = States.ENABLE;

        [Column("icon_url", TypeName = "varchar(255)"), Comment("ico图片url"), Required]
        public string IconUrl { get; set; }

        [Column("logo_url", TypeName = "varchar(255)"), Comment("logo图片url"), Required]
        public string LogoUrl { get; set; }
        
        [MaxLength(255), Comment("首页图标下文字")]
        public string IndexContent { get; set; }
        
        [DefaultValue(true), Comment("是否展示登陆页logo")]
        public bool ShowLoginLogo { get; set; }
        
        [DefaultValue(true), Comment("是否展示首页logo")]
        public bool ShowIndexLogo { get; set; }
        
        [DefaultValue(true), Comment("是否展示首页文字")]
        public bool ShowIndexContent { get; set; }
        
        /// <summary>
        /// 邮件接口设置
        /// </summary>
        public OemEmailSettings EmailSettings { get; set; }
        /// <summary>
        /// 首页ui设置json
        /// </summary>
        [Column(nameof(IndexUiSettings)), Comment("首页ui设置json"), MaxLength(255)]
        public string IndexUiSettingsJson { get; set; }

        [NotMapped]
        public IndexUiSettings IndexUiSettings => string.IsNullOrEmpty(IndexUiSettingsJson)
            ? new IndexUiSettings()
            : JsonConvert.DeserializeObject<IndexUiSettings>(IndexUiSettingsJson);
    }

    [Owned]
    public class OemEmailSettings
    {
        public int OemId { get; set; }
        [JsonIgnore]public Oem Oem { get; set; }
        
        /// <summary>
        /// 使用指定的第三方邮件接口
        /// </summary>
        public EmailPlatform EmailPlatform { get; set; } = EmailPlatform.Mailjet;

        /// <summary>
        /// 是否使用独立的mailjet接口
        /// </summary>
        public bool UseIndependentMailjet { get; set; } = false;
        [MaxLength(64)]
        public string MailjetApiKey { get; set; }
        [MaxLength(64)]
        public string MailjetApiSecret { get; set; }
        /// <summary>
        /// 是否使用独立的Rushmail接口
        /// </summary>
        public bool UseIndependentRushmail { get; set; } = false;
        [MaxLength(10)]
        public string RushmailCustomId { get; set; }
        [MaxLength(64)]
        public string RushmailSecretKey { get; set; }
    }

    public class IndexUiSettings
    {
        /// <summary>
        /// 首页第二行内容
        /// </summary>
        public string SecondContent { get; set; }
        
        /// <summary>
        /// 是否隐藏首页图片
        /// </summary>
        public bool HideImage { get; set; }
    }
}
